package libs;


import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import panel.PanelRuler;

public class GraphXYValue extends JPanel implements ChartMouseListener {

	private JLabel jLabelX;

	private String jConstLabelX;

	private float[] xValues;
	
	private String[][] xxValues;

	private JLabel jLabelY;

	private String jConstLabelY;

	private float[] yValues;
	private String[][] yyValues;
    private String[] expName;
    
	private ChartPanel chartpanel = null;

	private JFreeChart chart = null;

//	private float[] featureWeights;

	private static final long serialVersionUID = 1L;

	
//	public GraphXYValue(
//			String graphName, 
//			String labelX, 
//			String labelY,
//			float[] xValues, 
//			float[] yValues) {
//		setLayout(new BorderLayout());
//		this.xValues = xValues;
//		this.yValues = yValues;
//		XYSeriesCollection dataset = new XYSeriesCollection();
//		dataset.addSeries(getSeriesData());
//		chart = ChartFactory.createXYLineChart(graphName, labelX, labelY,
//				dataset, PlotOrientation.VERTICAL, false, false, false);
//		chart.setAntiAlias(true);
//		XYPlot plot = (XYPlot) chart.getPlot();
//		XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
//		renderer.setSeriesLinesVisible(0, true);
//		renderer.setSeriesShapesVisible(0, false);
//		renderer.setSeriesLinesVisible(1, false);
//		renderer.setSeriesShapesVisible(1, true);
//		renderer.setSeriesStroke(0, new BasicStroke(1.7F));
//		
//		chart.getXYPlot().setRenderer(new XYSplineRenderer());		
//		renderer.setItemLabelsVisible(true);		
//		
//		plot.setRenderer(renderer);
//		JPanel jpanel = new JPanel();
//		jConstLabelX = "Current Feature";//labelX;
//		jConstLabelY = labelY;
//		jLabelX = new JLabel("Current Feature");
//		jLabelY = new JLabel("");
//		jpanel.add(jLabelX);
//		jpanel.add(jLabelY);
//		add(jpanel, BorderLayout.SOUTH);
//		chartpanel = new ChartPanel(chart);
//		chartpanel.addChartMouseListener(this);
//		chartpanel.setVerticalAxisTrace(true);
//		chartpanel.setHorizontalAxisTrace(true);
//		chartpanel.setDomainZoomable(true);
//		chartpanel.setRangeZoomable(true);
//		chartpanel.setDisplayToolTips(false);
//		chartpanel.setFillZoomRectangle(true);
//		
//		add(chartpanel, BorderLayout.CENTER);
//
//	}

	
	private PanelRuler panelRankedBands=null;
	public JSlider slider_classification; 
	public JSlider slider_combination; 
	private String[] rankedBands;
	private int type=0; //0:classification,1:estimation
	
//	second constructor for estimation graph
	public GraphXYValue(
			String graphName, 
			String labelX, 
			String labelY,
			String[][] xxValues, 
			String[][] yyValues,
			String[] expName,
			String[] pepperNames,
			int type,
			JSlider slider
			) {
		
		this.type=type;
		if (type==2) {
			this.slider_combination=slider;
		}
		setLayout(new BorderLayout());
//		setLayout(new GridLayout(3,0));
		this.xxValues = xxValues;
		this.yyValues = yyValues;
		this.expName=expName;
		rankedBands=new String[pepperNames.length];
		for (int i = 0; i < rankedBands.length; i++) {
			//rankedBands[i]=xxValues[0][i]+"";
			rankedBands[i]=pepperNames[i];
		}

		//this.featureWeights=featureWeights;
		XYSeriesCollection dataset = new XYSeriesCollection();
		for (int i = 0; i < xxValues.length; i++) {
			dataset.addSeries(getMultiSeriesData(i));			
		}
		
		chart = ChartFactory.createXYLineChart(graphName, labelX, labelY,
				dataset,PlotOrientation.VERTICAL, true, true, false);
		chart.setAntiAlias(true);
		XYPlot plot = (XYPlot) chart.getPlot();
		XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		for (int i = 0; i < yyValues.length; i++) {
			//b.sadece line ciziyor, kesisim noktalarini koymuyor
			renderer.setSeriesShapesVisible(i, true);
			
			//renderer.setSeriesShapesVisible(i, true);
            renderer.setBaseShapesVisible(true);
            renderer.setBaseShapesFilled(true);
            renderer.setDrawSeriesLineAsPath(true);
			//renderer.setItemLabelsVisible(false);
		}
		plot.setRenderer(renderer);		
		
		
//		add(panelRankedBands);
		
		chartpanel = new ChartPanel(chart);
		chartpanel.addChartMouseListener(this);
		chartpanel.setVerticalAxisTrace(true);
		chartpanel.setHorizontalAxisTrace(true);
		chartpanel.setDomainZoomable(true);
		chartpanel.setRangeZoomable(true);
		chartpanel.setDisplayToolTips(false);
		chartpanel.setFillZoomRectangle(true);	
		add(chartpanel, BorderLayout.CENTER);
//		add(chartpanel);
		
//		panelRankedBands=new myRulerPanel(rankedBands);
//		panelRankedBands.setPreferredSize(new Dimension(1000,40));
//		add(panelRankedBands,BorderLayout.NORTH);
		
		JPanel jpanel = new JPanel();
		jpanel.setLayout(new GridLayout(3,0));
		jConstLabelX = "Current Feature";//labelX;
		jConstLabelY = labelY;
		jLabelX = new JLabel("");
		jLabelY = new JLabel("");
		jpanel.add(jLabelX);
		jpanel.add(jLabelY);
		add(jpanel, BorderLayout.SOUTH);
//		add(jpanel);
	}


	public GraphXYValue(
			String graphName, 
			String labelX, 
			String labelY,
			String[][] xxValues, 
			String[][] yyValues,
			String[] expName,
			JSlider slider,
			int type) {
		this.type=type;
		this.slider_classification=slider;
		setLayout(new BorderLayout());
//		setLayout(new GridLayout(3,0));
		this.xxValues = xxValues;
		this.yyValues = yyValues;
		this.expName=expName;
		rankedBands=new String[xxValues[0].length];
		for (int i = 0; i < rankedBands.length; i++) {
			rankedBands[i]=xxValues[0][i]+"";
		}

		//this.featureWeights=featureWeights;
		XYSeriesCollection dataset = new XYSeriesCollection();
		for (int i = 0; i < xxValues.length; i++) {
			dataset.addSeries(getMultiSeriesData(i));			
		}
		
		chart = ChartFactory.createXYLineChart(graphName, labelX, labelY,
				dataset,PlotOrientation.VERTICAL, true, true, false);
//		chart = ChartFactory.createLineChart(graphName, labelX, labelY,
//				dataset,PlotOrientation.VERTICAL, true, true, false);
		chart.setAntiAlias(true);
		XYPlot plot = (XYPlot) chart.getPlot();
//		CategoryPlot plot = (CategoryPlot) chart.getPlot();
        
        
		XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
//		LineAndShapeRenderer renderer = new LineAndShapeRenderer();
		Color[] renkler={Color.red,Color.green,Color.blue,Color.magenta,Color.cyan,Color.orange,Color.pink,Color.yellow};
		for (int i = 0; i < yyValues.length; i++) {
			//b.sadece line ciziyor, kesisim noktalarini koymuyor
			renderer.setSeriesShapesVisible(i, true);
			
			//renderer.setSeriesShapesVisible(i, true);
            renderer.setBaseShapesVisible(true);
            renderer.setBaseShapesFilled(true);
            renderer.setDrawSeriesLineAsPath(true);
			//renderer.setItemLabelsVisible(false);
            renderer.setSeriesPaint(i,renkler[i]);
            
		}
		plot.setRenderer(renderer);		
		
		
//		add(panelRankedBands);
		
		chartpanel = new ChartPanel(chart);
		chartpanel.addChartMouseListener(this);
		chartpanel.setVerticalAxisTrace(true);
		chartpanel.setHorizontalAxisTrace(true);
		chartpanel.setDomainZoomable(true);
		chartpanel.setRangeZoomable(true);
		chartpanel.setDisplayToolTips(false);
		chartpanel.setFillZoomRectangle(true);	
		add(chartpanel, BorderLayout.CENTER);
//		add(chartpanel);
		
		if (this.type!=-1) {
			panelRankedBands=new PanelRuler(rankedBands);
			panelRankedBands.setPreferredSize(new Dimension(1000,40));
			add(panelRankedBands,BorderLayout.NORTH);
		}
		
		JPanel jpanel = new JPanel();
		jpanel.setLayout(new GridLayout(3,0));
		jConstLabelX = "Current Feature";//labelX;
		jConstLabelY = labelY;
		jLabelX = new JLabel("");
		jLabelY = new JLabel("");
		jpanel.add(jLabelX);
		jpanel.add(jLabelY);
		add(jpanel, BorderLayout.SOUTH);
//		add(jpanel);
	}



	public XYSeries getSeriesData() {
		XYSeries series = new XYSeries("XYGraph", false, true);
		for (int i = 0; i < yValues.length; i++) {
			series.add(xValues[i], yValues[i]);
		}
		return series;
	}
	
	public XYSeries getMultiSeriesData(int n) {
		XYSeries series = new XYSeries(expName[n]);
		//println("");
		for (int i = 0; i < yyValues[n].length; i++) {
			//series.add(xxValues[n][i], yyValues[n][i]);
			series.add(i+1,Double.parseDouble(yyValues[n][i]));
			//println(yyValues[n][i]);
		}
		return series;
	}

//	private void println(String str) {
//		System.out.println(str);
//		
//	}


	// onemli Grafik koordinatlari bulunup labellere koyuluyor.
	public void chartMouseMoved(ChartMouseEvent event) {

		if (!chart.getTitle().getText().equalsIgnoreCase("Polar Chart")) {

			int mouseX = event.getTrigger().getX();
			int mouseY = event.getTrigger().getY();
			//panel kordinatlari alinip chart kordinatlara donusturuluyor.
			Point2D p = chartpanel.translateScreenToJava2D(new Point(mouseX,mouseY));
			XYPlot plot = (XYPlot) event.getChart().getPlot();
//			CategoryPlot plot = (CategoryPlot) event.getChart().getPlot();
			Rectangle2D plotArea = chartpanel.getChartRenderingInfo().getPlotInfo().getDataArea();
			ValueAxis domainAxis = plot.getDomainAxis();
//			CategoryAxis domainAxis = plot.getDomainAxis();
			org.jfree.ui.RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
			ValueAxis rangeAxis = plot.getRangeAxis();
			org.jfree.ui.RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
			double chartX = domainAxis.java2DToValue(p.getX(), plotArea,domainAxisEdge);
			double chartY = rangeAxis.java2DToValue(p.getY(), plotArea,rangeAxisEdge);
			double chartMaxX = domainAxis.java2DToValue(plotArea.getMaxX(),plotArea, domainAxisEdge);
			double chartMinX = domainAxis.java2DToValue(plotArea.getMinX(),plotArea, domainAxisEdge);
			double chartMaxY = rangeAxis.java2DToValue(plotArea.getMinY(),plotArea, rangeAxisEdge);
			double chartMinY = rangeAxis.java2DToValue(plotArea.getMaxY(),plotArea, rangeAxisEdge);
			
			String chX=roundSpectralBandValue(chartX);
			if (chartX >= chartMinX && chartX <= chartMaxX
					&& chartY >= chartMinY && chartY <= chartMaxY) {
				jLabelX.setText(jConstLabelX + " : " + chX);
				jLabelY.setText(jConstLabelY + " : " + chartY);
				int n=getIndexOfSpectralBands(chX+"");
				if (this.type==0) {
					panelRankedBands.setSelectedClassLabels(n);
					slider_classification.setValue(n+1);
				}
				//if estimation was selected
				if (this.type==1) {
					try {
						jLabelX.setText("Current Pepper : " + rankedBands[Integer.parseInt(chX)-1]);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				//if combination was selected
				if (this.type==2) {
					try {
						jLabelX.setText("Current Combination : " + chX);
						slider_combination.setValue(Integer.parseInt(chX));
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				
			}

		}
	}

	private int getIndexOfSpectralBands(String chx) {
		if (rankedBands==null || rankedBands.length==0) {
			return -1;
		}
//		System.out.println("chx:"+chx);
//		System.out.println("rnk:"+rankedBands[0]);
		
		int n=0;
		for (int i = 0; i < rankedBands.length; i++) {			
			if (chx.equals(rankedBands[i])) {
				return n;
			}
			n++;
		}
		return -2;
	}


	private String roundSpectralBandValue(double chartX) {
		String ret="";
		int x=(int)Math.round(chartX);	
		try {
			ret=(xxValues[0][x-1]);
		} catch (Exception e) {
			// TODO: handle exception
		}		
		return ret;
	}

	public void chartMouseClicked(ChartMouseEvent event) {
		// TODO Auto-generated method stub

	}

}
